//
//  SelectTag_NewViewController.m
//  HAODE
//
//  Created by LiYinDi on 16/2/2.
//  Copyright © 2016年 reabam.com. All rights reserved.
//

#import "SelectTagViewController.h"

#import "HttpRequest.h"
#import "AddTagViewController.h"

@interface SelectTagViewController ()
{
    NSMutableArray *tagNameArray;
    NSMutableArray *tagCodeArray;
    NSMutableArray *selectTagCodeArray;
    NSMutableArray *selectTagNameArray;
//    ReabamSingleton*commdity;
}
@end

@implementation SelectTagViewController
-(void)viewWillAppear:(BOOL)animated
{
    tagNameArray=[NSMutableArray array];
    tagCodeArray=[NSMutableArray array];
    selectTagCodeArray=[NSMutableArray array];
    selectTagNameArray=[NSMutableArray array];
//    commdity=[ReabamSingleton shared];
    [self setNavigation];
    [self setAutolayout];
    [self requestTag];

}
- (void)viewDidLoad {
    [super viewDidLoad];
   
  }
-(void)requestTag
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    if (Token ==nil) {
        Token=@"dd";
    }
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:_optionNameString forKey:@"optionName"];
    [Global showIndeterminateHud:@"加载中，请稍后。。。" delay:30 view:self.view];
    [HttpRequest requestData:@"Common/AppCode/List" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
       // NSLog(@"////===%@",severDataDict.dataDict);
        if (isSuccess) {
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                 [Global hideProgressHUDInternal:self.view];
                NSMutableArray *dataArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"DataLine"]];
                for (int i=0; i<dataArray.count; i++) {
                    [tagCodeArray addObject:[[dataArray objectAtIndex:i] objectForKey:@"code"]];
                    [tagNameArray addObject:[[dataArray objectAtIndex:i] objectForKey:@"content"]];
                }
                
                [self setTagView];
                
                
                //
            }else{
                [Global hideProgressHUDInternal:self.view];
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    
                    
                    //                    TheLoginViewController *detailViewController = [[TheLoginViewController alloc] init];
                    //                    [self.navigationController pushViewController:detailViewController animated:YES];
                }
                
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];

}
-(void)setTagView
{
    for (UIView *v in _TagView.subviews ) {
        [v removeFromSuperview];
    }
    for (int i=0; i<tagNameArray.count; i++) {
        
        UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
        CGRect rect=button.frame;
        rect.size.height=32;
         rect.size.width=ScreenWidth/4.5;
         rect.origin.x=(ScreenWidth/4.2)*(i%4);
        rect.origin.y=12+40*(i/4);
        button.frame=rect;
        button.tag=i;
        [button setBackgroundColor:[UIColor clearColor]];
        button.layer.borderWidth=1;
        button.layer.borderColor=[[UIColor colorWithRed:239.0f/255.0f green:239.0f/255.0f blue:239.0f/255.0f alpha:1.0f] CGColor];
        button.layer.cornerRadius=16;
        button.titleLabel.font = [UIFont systemFontOfSize: 15.0];
        [button setTitle:[tagNameArray objectAtIndex:i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithRed:100.0f/255.0f green:100.0f/255.0f blue:100.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
        if (_selectedTagCodeArray.count>0) {
            for (int j=0; j<_selectedTagCodeArray.count; j++) {
                if ([[tagCodeArray objectAtIndex:i] isEqualToString:[_selectedTagCodeArray objectAtIndex:j]]) {
                    // button.layer.borderColor=[[UIColor colorWithRed:252.0f/255.0f green:165.0f/255.0f blue:0.0f/255.0f alpha:1] CGColor];
                    button.layer.borderWidth= 1.0f;
                    [button setBackgroundColor:[UIColor colorWithRed:254.0f/255.0f green:249.0f/255.0f blue:228.0f/255.0f alpha:1.0f]];
                    //  button.layer.borderWidth=1;
                    button.layer.borderColor=[[UIColor colorWithRed:226.0f/255.0f green:208.0f/255.0f blue:166.0f/255.0f alpha:1.0f] CGColor];
                    [button setTitleColor:[UIColor colorWithRed:209.0f/255.0f green:174.0f/255.0f blue:80.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
                    [selectTagCodeArray addObject:[tagCodeArray objectAtIndex:i]];
                    [selectTagNameArray addObject:[tagNameArray objectAtIndex:i]];
                    
                }
            }
            [_countView setBackgroundColor:[UIColor colorWithRed:24.0f/255.0f green:24.0f/255.0f blue:24.0f/255.0f alpha:0.88]];

        }
        
        else
        {
            [_countView setBackgroundColor:[UIColor colorWithRed:225.0f/255.0f green:225.0f/255.0f blue:225.0f/255.0f alpha:1.0]];
        }

        [button addTarget:self action:@selector(tagButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_TagView addSubview:button];
    }
    
    _countLabel.text=[NSString stringWithFormat:@"已选 %lu 个",(unsigned long)selectTagCodeArray.count];
}
-(void)tagButtonClick:(UIButton *)sender
{
    //sender.layer.borderColor=[[UIColor colorWithRed:252.0f/255.0f green:165.0f/255.0f blue:0.0f/255.0f alpha:1] CGColor];
    [sender setBackgroundColor:[UIColor colorWithRed:254.0f/255.0f green:249.0f/255.0f blue:228.0f/255.0f alpha:1.0f]];
    sender.layer.borderWidth=1;
    sender.layer.borderColor=[[UIColor colorWithRed:226.0f/255.0f green:208.0f/255.0f blue:166.0f/255.0f alpha:1.0f] CGColor];
    [sender setTitleColor:[UIColor colorWithRed:209.0f/255.0f green:174.0f/255.0f blue:80.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
    
    // sender.layer.borderWidth= 1.0f;
    if (![selectTagCodeArray containsObject:[tagCodeArray objectAtIndex:sender.tag]]) {
        [selectTagCodeArray addObject:[tagCodeArray objectAtIndex:sender.tag]];
        [selectTagNameArray addObject:[tagNameArray objectAtIndex:sender.tag]];
        // sender.layer.borderColor=[[UIColor colorWithRed:252.0f/255.0f green:165.0f/255.0f blue:0.0f/255.0f alpha:1] CGColor];
        sender.layer.borderColor=[[UIColor colorWithRed:226.0f/255.0f green:208.0f/255.0f blue:166.0f/255.0f alpha:1.0f] CGColor];
        sender.layer.borderWidth= 1.0f;
        [sender setTitleColor:[UIColor colorWithRed:209.0f/255.0f green:174.0f/255.0f blue:80.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
    }
    else
    {
        [selectTagCodeArray removeObject:[tagCodeArray objectAtIndex:sender.tag]];
         [selectTagNameArray removeObject:[tagNameArray objectAtIndex:sender.tag]];
        sender.layer.borderColor=[[UIColor colorWithRed:239.0f/255.0f green:239.0f/255.0f blue:239.0f/255.0f alpha:1.0f] CGColor];
        sender.layer.borderWidth= 1.0f;
        [sender setBackgroundColor:[UIColor clearColor]];
        [sender setTitleColor:[UIColor colorWithRed:100.0f/255.0f green:100.0f/255.0f blue:100.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
    }
    // NSLog(@"selectCodeArray====%@",selectCodeArray);
    _countLabel.text=[NSString stringWithFormat:@"已选 %lu 个",(unsigned long)selectTagCodeArray.count];
    
    if (selectTagCodeArray.count==0) {
        [_countView setBackgroundColor:[UIColor colorWithRed:225.0f/255.0f green:225.0f/255.0f blue:225.0f/255.0f alpha:1.0]];
    }
    else
    {
         [_countView setBackgroundColor:[UIColor colorWithRed:24.0f/255.0f green:24.0f/255.0f blue:24.0f/255.0f alpha:0.88]];
    }
}

- (IBAction)confirmBtnClick:(id)sender {
    //selectTagCodeArray
    //selectTagNameArray
//    commdity.selectTagCodeArray=[selectTagCodeArray copy];
//
//    commdity.selectTagNameArray=[selectTagNameArray copy];
//    NSLog(@"------%@",selectTagCodeArray);
   // NSLog(@"------%@",selectTagNameArray);
    [self.navigationController popViewControllerAnimated:YES];

    
    
    
}

-(void)setNavigation
{
    
       UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(BackAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    
    UIButton*addBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0, 30, 30)];
    [addBtn setImage:[UIImage imageNamed:@"tianjia"] forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(addBtnClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* addBtnItem = [[UIBarButtonItem alloc] initWithCustomView:addBtn];
   
    self.navigationItem.rightBarButtonItem = addBtnItem;
    
    UILabel* titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0,40, 23)];
    titleLabel.text=@"选择标签";
    titleLabel.font=[UIFont systemFontOfSize: 19];
    self.navigationItem.titleView=titleLabel;
}
-(void)BackAction
{
     [self.navigationController popViewControllerAnimated:YES];
}
-(void)addBtnClick
{
    AddTagViewController *addTagVC=[[AddTagViewController alloc] init];
    addTagVC.optionNameStr=self.optionNameString;
    [self.navigationController pushViewController:addTagVC animated:YES];
}

-(void)setAutolayout
{
    [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.height.equalTo(@50);
        make.top.equalTo(self.view.mas_top).with.offset(64);
    }];
    [_topLineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_topView.mas_left).with.offset(10);
        make.right.equalTo(_topView.mas_right).with.offset(-10);
        make.height.equalTo(@1);
        make.bottom.equalTo(self.view.mas_bottom);
 
    }];
    [_TagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).with.offset(10);
        make.right.equalTo(self.view.mas_right).with.offset(-10);
        make.top.equalTo(_topView.mas_bottom) ;
        make.bottom.equalTo(_bottomView.mas_top);
    }];
    
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.height.equalTo(@50) ;
        make.bottom.equalTo(self.view.mas_bottom);
    }];
    
    [_confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(_bottomView.mas_right) ;
        make.width.equalTo(@140) ;
        make.height.equalTo(@50) ;
        make.bottom.equalTo(_bottomView.mas_bottom);
    }];
    [_countView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_bottomView.mas_left) ;
        make.right.equalTo(_confirmBtn.mas_left) ;
        make.height.equalTo(@50) ;
        make.bottom.equalTo(_bottomView.mas_bottom);
    }];


}
@end
